﻿using System;
using Microsoft.VisualBasic.ApplicationServices;

namespace UploadClient
{
    //The Main entry point class
    public class Startup
    {
        [STAThread]
        public static void Main(string[] args)
        {
            SingleInstanceAppWrapper wrapper = new SingleInstanceAppWrapper();
            wrapper.Run(args);
        }
    }
 
    //The old-style application wrapper
    public class SingleInstanceAppWrapper : WindowsFormsApplicationBase
    {
        // Create the WPF application class.
        private App _app;

        public SingleInstanceAppWrapper()
        {
            // Enable single-instance mode.
            this.IsSingleInstance = true;
        }

        //Override OnStartup() method to create the WPF application object
        protected override bool OnStartup(
             StartupEventArgs e)
        {
            _app = new App();

            try
            {
                _app.Run();
            }
            catch (Exception ex)
            {
                //ToDo: prevent closing!
                ExceptionHandling.ThrowMessage(new CustomKeyException("ERR_0000", ex));
            }

            return false;
        }
 
        // Override OnStartupNextInstance() to handle multiple application instances.
        protected override void OnStartupNextInstance(StartupNextInstanceEventArgs e)
        {
            //In case of command line arguments, send them to the WPF application object    
            if (e.CommandLine.Count > 0)
            {
                _app.HandleCommandLine(e.CommandLine);
            }
        }
    }
}
